package com.woniuxy.microfixeddepositserver.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.woniuxy.bean.ResultBean;
import com.woniuxy.bean.bank.BankBean;
import com.woniuxy.microfixeddepositserver.bean.FundManagerBean;
import com.woniuxy.microfixeddepositserver.service.IFundManagerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.ObjectUtils;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: masterhai
 * @date: 2021/4/9:12:10
 * @description:
 */
@RefreshScope
@RestController
@Slf4j
@Api("基金经理操作接口")
@RequestMapping(value = "/fundManager", produces = {"application/json;charset=utf-8"})
public class FundManagerController {
    @Resource
    private IFundManagerService fundManagerService;

    /**
     * 查询基金经理信息
     *
     * @return
     */
    @PostMapping("add")
    public ResultBean addManager (@ApiParam("基金经理对象") @RequestBody FundManagerBean fundManagerBean) {
        if (fundManagerBean == null) {
            return ResultBean.ParamError();
        }
        fundManagerService.addManager(fundManagerBean);
        return ResultBean.builder().code(200).msg("新增基金经理数据成功").build();
    }

    /**
     * 查询基金经理信息
     *
     * @return
     */
    @PostMapping("info/{page}/{limit}")
    public ResultBean selectAll (@ApiParam("当前页") @PathVariable(value = "page", required = false) Integer page,
                             @ApiParam("每页条数") @PathVariable(value = "limit", required = false) Integer limit,
                             @RequestBody FundManagerBean fundManagerBean) {
        Integer page1 = page == null ? 1 : page;
        Integer limit1 = limit == null ? 5 : limit;
        PageInfo<FundManagerBean> pageInfo = null;
        if (fundManagerBean!=null && fundManagerBean.getMname()!=null && !"".equals(fundManagerBean.getMname())) {
            //根据基金经理名字查询
            pageInfo = fundManagerService.findByName(page1, limit1, fundManagerBean);
        } else {
            pageInfo = fundManagerService.selectAll(page1, limit1);
        }

        return ResultBean.builder().code(200).msg("查询基金经理数据成功").data(pageInfo).build();
    }

    @ApiOperation("定期产品基金经理支持接口")
    @PostMapping("infos")
    public ResultBean selectAllInfos () {

           List<FundManagerBean> beans = fundManagerService.selectAllInfos();


        return ResultBean.builder().code(200).msg("查询基金经理数据成功").data(beans).build();
    }

    /**
     * 修改基金经理信息
     *
     * @return
     */
    @PostMapping("update")
    public ResultBean updateManager (@ApiParam("基金经理对象") @RequestBody FundManagerBean fundManagerBean) {
        if (fundManagerBean == null) {
            return ResultBean.ParamError();
        }
        fundManagerService.updateByPrimaryKey(fundManagerBean);
        return ResultBean.builder().code(200).msg("修改基金经理数据成功").build();
    }

    /**
     * 删除基金经理信息
     *
     * @return
     */
    @GetMapping("delete/{id}")
    public ResultBean deleteManager (@ApiParam("基金经理id") @PathVariable("id") Integer id) {
        if (id != null && id < 1) {
            ResultBean.ParamError();
        }
        fundManagerService.deleteByPrimaryKey(id);
        return ResultBean.builder().code(200).msg("删除基金经理数据成功").build();
    }

    @ApiOperation("查询基金经理信息")
    @GetMapping("getOne/{id}")
    public ResultBean getBankById (@ApiParam("基金经理id") @PathVariable Integer id) {
        if (id == null || "".equals(id)) {
            return ResultBean.ParamError();
        }
        FundManagerBean fundManagerBean = fundManagerService.getManagerById(id);
        return ResultBean.builder().code(200).msg("查询基金经理数据成功").data(fundManagerBean).build();
    }
}
